package com.example.qts.fragments;


import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.qts.R;
import com.example.qts.adapter.DishAdapter;
import com.example.qts.base.BaseFragment;
import com.example.qts.bean.Bean;
import com.example.qts.presenter.ListPresenter;
import com.example.qts.view.ListView;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class DishFragment extends BaseFragment<ListPresenter> implements ListView {
    @BindView(R.id.dish_recycler)
    RecyclerView mdish_recycler;
    private ArrayList<Bean.DataBean> dataBeans;
    private DishAdapter dishAdapter;
    //懒加载
    @Override 
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser){
            mPresenter.getdata();
        }
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        mdish_recycler.setLayoutManager(new LinearLayoutManager(getContext()));
        mdish_recycler.addItemDecoration(new DividerItemDecoration(getContext(),LinearLayoutManager.VERTICAL));
        dataBeans = new ArrayList<>();
        dishAdapter = new DishAdapter(getContext(), dataBeans);
        mdish_recycler.setAdapter(dishAdapter);
    }

    @Override
    protected void initPresenter() {
        mPresenter=new ListPresenter();
    }

    @Override
    protected int getLyout() {
        return R.layout.fragment_dish;
    }

    @Override
    public void onSuccess(Bean data) {
        dataBeans.addAll(data.getData());
        dishAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFali(String error) {
        Toast.makeText(getContext(),error,Toast.LENGTH_LONG).show();
    }

    @Override
    public void showToast(String str) {
        Toast.makeText(getContext(),str,Toast.LENGTH_LONG).show();

    }
}
